﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DataCentral.UsecaseControllers;
using DevExpress.XtraBars;
using DataCentral.BusinessDataObjects;
using DevExpress.XtraBars.Docking;
using System.Net.NetworkInformation;
using DataCentral.Dataset;
using DataCentral.UserControls;
using DataCentral.Dataset.DataCentralDatasetTableAdapters;
using CommonData;

namespace DataCentral
{
    public partial class MainForm : DevExpress.XtraEditors.XtraForm
    {
        public MainUsecase _UCMain;
        public UserBO _UserBO = null;
        private bool _IsLogout = false;
        public UserBO UserLoginBO
        {
            get { return _UserBO; }
            set { _UserBO = value; }
        }

        public MainForm(MainUsecase _UCMain, UserBO _UserBO)
        {
            InitializeComponent();
            this._UCMain = _UCMain;
            this._UserBO = _UserBO;            
        }

        

        public void InitAllFormInfomation()
        {
            barManager.GetController().Changed += new EventHandler(ChangedController);
            IPS_Init();
            InitSkins();
            InitFormInformation();
            _UCMain.ProcessAccessRight(barManager, navBarControl, _UserBO);
            _IsLogout = false;
            aboutMenuItem.Enabled = true;
            versionSubMenu.Enabled = true;
        }

        

        #region Methods


        private void InitFormInformation()
        {
            lblUserLogin.Caption = string.Format("Tài khoản: {0}",_UserBO.UserName);
            lblUserRole.Caption = string.Format("Quyền: {0}", _UserBO.GetUserRoleName(_UserBO.RoleID));
            bool isOnline = NetworkInterface.GetIsNetworkAvailable();
            iOnline.Visibility = isOnline ? BarItemVisibility.Always : BarItemVisibility.Never;
            iOffline.Visibility = isOnline ? BarItemVisibility.Never : BarItemVisibility.Always;
            NetworkChange.NetworkAvailabilityChanged += new NetworkAvailabilityChangedEventHandler(NetworkChange_NetworkAvailabilityChanged);
        }

        void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
        {
            iOnline.Visibility = e.IsAvailable ? BarItemVisibility.Always : BarItemVisibility.Never;
            iOffline.Visibility = e.IsAvailable ? BarItemVisibility.Never : BarItemVisibility.Always;
        }

        #endregion

        #region Properties
        public GroupControl CenterGroup
        {
            get { return grpMain; }
            set { grpMain = value; }
        }
        #endregion

        #region Skins
        private void IPS_Init()
        {
            BarItem item = null;
            for (int i = 0; i < barManager.Items.Count; i++)
                if (barManager.Items[i].Description == barManager.GetController().PaintStyleName)
                    item = barManager.Items[i];
            InitPaintStyle(item);
        }


        string skinMask = "Giao diện: ";
        bool skinProcessing = false;
        private void InitPaintStyle(BarItem item)
        {
            if (item == null) return;
            iPaintStyle.ImageIndex = item.ImageIndex;
            iPaintStyle.Caption = item.Caption;
            iPaintStyle.Hint = item.Description;
        }

        void InitSkins()
        {
            barManager.ForceInitialize();
            if (barManager.GetController().PaintStyleName == "Giao diện")
                iPaintStyle.Caption = skinMask + DevExpress.LookAndFeel.UserLookAndFeel.Default.ActiveSkinName;
            foreach (DevExpress.Skins.SkinContainer cnt in DevExpress.Skins.SkinManager.Default.Skins)
            {
                BarButtonItem item = new BarButtonItem(barManager, skinMask + cnt.SkinName);
                //item.Category.Name = "iPaintStyle";
                iPaintStyle.AddItem(item);
                item.ItemClick += new ItemClickEventHandler(OnSkinClick);
            }
        }
        void OnSkinClick(object sender, ItemClickEventArgs e)
        {
            string skinName = e.Item.Caption.Replace(skinMask, "");
            DevExpress.LookAndFeel.UserLookAndFeel.Default.SetSkinStyle(skinName);
            barManager.GetController().PaintStyleName = "Giao diện";
            iPaintStyle.Caption = e.Item.Caption;
            iPaintStyle.Hint = iPaintStyle.Caption;
            iPaintStyle.ImageIndex = -1;
        }

        private void IPS_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            barManager.GetController().PaintStyleName = e.Item.Description;
            InitPaintStyle(e.Item);
            barManager.GetController().ResetStyleDefaults();
            DevExpress.LookAndFeel.UserLookAndFeel.Default.SetDefaultStyle();
        }

        private void ChangedController(object sender, EventArgs e)
        {
            if (skinProcessing) return;
            string paintStyleName = barManager.GetController().PaintStyleName;
            if ("Mặc định".IndexOf(paintStyleName) >= 0)
                DevExpress.Skins.SkinManager.EnableFormSkins();
            else DevExpress.Skins.SkinManager.DisableFormSkins();
            skinProcessing = true;
            DevExpress.LookAndFeel.LookAndFeelHelper.ForceDefaultLookAndFeelChanged();
            skinProcessing = false;
        }
        #endregion

        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (!_IsLogout)
                Environment.Exit(0);
        }


        #region Visible docking control
        private void dockLeft_DockChanged(object sender, EventArgs e)
        {
            iShowLeft.Checked = dockLeft.Visibility == DockVisibility.Visible;
        }

        private void dockRight_DockChanged(object sender, EventArgs e)
        {
            //iShowRight.Checked = dockRight.Visibility == DockVisibility.Visible;
        }

        private void iShowLeft_CheckedChanged(object sender, ItemClickEventArgs e)
        {
            if (iShowLeft.Checked)
                dockLeft.Visibility = DockVisibility.Visible;
            else
                dockLeft.Visibility = DockVisibility.AutoHide;
        }

        private void iShowRight_CheckedChanged(object sender, ItemClickEventArgs e)
        {
            //if (iShowRight.Checked)
            //    dockRight.Visibility = DockVisibility.Visible;
            //else
            //    dockRight.Visibility = DockVisibility.AutoHide;
        }
        #endregion

        private void iLogout_ItemClick(object sender, ItemClickEventArgs e)
        {
            _IsLogout = true;
            Close();
            _UCMain.UserLogout();
        }

        private void iUserMaintenance_ItemClick(object sender, ItemClickEventArgs e)
        {
            _UCMain.ShowUserMaintenance();
        }

        private void iRoleMaintenance_ItemClick(object sender, ItemClickEventArgs e)
        {
            _UCMain.ShowRoleMaintenance();
        }

        private void iChangePassword_ItemClick(object sender, ItemClickEventArgs e)
        {
            _UCMain.ShowChangePasswordForm(_UserBO);
        }


        private void iProfile_ItemClick(object sender, ItemClickEventArgs e)
        {
            _UCMain.ShowProfileForm(_UserBO);
        }

        private void iExit_ItemClick(object sender, ItemClickEventArgs e)
        {
            Environment.Exit(0);
        }

        private void iSite_ItemClick(object sender, ItemClickEventArgs e)
        {
        }

        private void iAccount_ItemClick(object sender, ItemClickEventArgs e)
        {
        }

        private void iCalc_ItemClick(object sender, ItemClickEventArgs e)
        {
            System.Diagnostics.Process.Start("calc.exe");
        }

        private void iWorldTime_ItemClick(object sender, ItemClickEventArgs e)
        {
            _UCMain.ShowWorldTimeForm();
        }

        private void iException_ItemClick(object sender, ItemClickEventArgs e)
        {
            _UCMain.ShowExceptionManagement();
        }

        private void iWebsiteConfiguration_ItemClick(object sender, ItemClickEventArgs e)
        {
        }


        private void iModule_ItemClick(object sender, ItemClickEventArgs e)
        {
            _UCMain.ShowModuleMaintenanceControl();
        }


        private void iConfigEmail_ItemClick(object sender, ItemClickEventArgs e)
        {
            _UCMain.ShowConfigAutoSendMailForm();
        }

        private void iAccessRight_ItemClick(object sender, ItemClickEventArgs e)
        {
            _UCMain.ShowAccessRightForm();
        }

        private void iConfigDatabase_ItemClick(object sender, ItemClickEventArgs e)
        {
            _UCMain.ShowConfigDatabaseForm();
        }


        private void iDatabaseConfiguration_ItemClick(object sender, ItemClickEventArgs e)
        {
            _UCMain.ShowDatabaseConfigurationControl();
        }


        private void iSynchronize_ItemClick(object sender, ItemClickEventArgs e)
        {
            _UCMain.ShowSynchronizeControl();
        }


        private void iAfterSync_ItemClick(object sender, ItemClickEventArgs e)
        {
            _UCMain.ShowAfterSyncForm();
        }


        private void iSaleTeam_ItemClick(object sender, ItemClickEventArgs e)
        {
            _UCMain.ShowSaleTeamControl();
        }


        private void iAssignSalePerson_ItemClick(object sender, ItemClickEventArgs e)
        {
            _UCMain.ShowAssignSalePersonControl();
        }


        private void iEditSalePerson_ItemClick_1(object sender, ItemClickEventArgs e)
        {
            _UCMain.ShowEditSalePersonControl();
        }

        private void iAssignRoute_ItemClick(object sender, ItemClickEventArgs e)
        {
            _UCMain.ShowAssignRouteControl();
        }


        private void iEditSaleOrders_ItemClick(object sender, ItemClickEventArgs e)
        {
            _UCMain.ShowEditSaleOrderControl();
        }

        private void iEditSaleOrderDetail_ItemClick(object sender, ItemClickEventArgs e)
        {
            _UCMain.ShowEditSaleOrderDetailControl();
        }

        private void iConvertSaleOrder_ItemClick(object sender, ItemClickEventArgs e)
        {
            _UCMain.ShowConvertSaleOrderControl();
        }


        private void iDebtAdjust_ItemClick(object sender, ItemClickEventArgs e)
        {
            _UCMain.ShowDebtAdjustControl();
        }

        private void iPerformDebt_ItemClick(object sender, ItemClickEventArgs e)
        {
            _UCMain.ShowPerformDebtForm();
        }

        private void iSalePersonDebt_ItemClick(object sender, ItemClickEventArgs e)
        {
            _UCMain.ShowSalePersonDebtControl();
        }

        #region Navbar Item Processing
        private void nLogout_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            iLogout_ItemClick(null, null);
        }

        private void nUserMaintenance_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            iUserMaintenance_ItemClick(null, null);
        }

        private void nRoleMaintenance_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            iRoleMaintenance_ItemClick(null, null);
        }

        private void nChangePassword_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            iChangePassword_ItemClick(null, null);
        }

        private void nAccessRight_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            iAccessRight_ItemClick(null, null);
        }

        private void nProfile_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            iProfile_ItemClick(null, null);
        }

        private void nBetting_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
        }

        private void nSite_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            iSite_ItemClick(null, null);
        }

        private void nAccount_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            iAccount_ItemClick(null, null);
        }

        private void nModule_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            iModule_ItemClick(null, null);
        }

        private void nException_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            iException_ItemClick(null, null);
        }

        private void nSynchronize_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            iSynchronize_ItemClick(null,null);
        }

        private void nConvertSaleOrder_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            iConvertSaleOrder_ItemClick(null, null);
        }

        private void nDebtAdjust_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            iDebtAdjust_ItemClick(null, null);
        }

        private void nSalePersonDebt_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            iSalePersonDebt_ItemClick(null, null);
        }

        private void nPerformDebt_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            iPerformDebt_ItemClick(null, null);
        }

        private void nAfterSync_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            iAfterSync_ItemClick(null, null);
        }

        private void nSaleTeam_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            iSaleTeam_ItemClick(null, null);
        }

        private void nEditSalePerson_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            iEditSalePerson_ItemClick_1(null, null);
        }

        private void nAssignSalePerson_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            iAssignSalePerson_ItemClick(null, null);
        }

        private void nAssignRoute_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            iAssignRoute_ItemClick(null, null);
        }

        private void nEditSaleOrders_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            iEditSaleOrders_ItemClick(null, null);
        }

        private void nEditSaleOrderDetail_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            iEditSaleOrderDetail_ItemClick(null, null);
        }
        #endregion

        private void MainForm_Shown(object sender, EventArgs e)
        {
            this.Cursor = System.Windows.Forms.Cursors.Default;
        }

        private void nConfigDatabase_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            iConfigDatabase_ItemClick(null, null);
        }

        

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            MessageClass.ShowInformationMessage("Dữ liệu sẽ được lưu xuống cơ sở dữ liệu trước khi tắt chương trình.");
        }

        /// <summary>
        /// Phiên bản
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barSubItem2_ItemClick(object sender, ItemClickEventArgs e)
        {
            //evaluate.bettingBotMain.bettingBotSBOError.Login_Test();
            About frm = new About();
            frm.ShowDialog();
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            DigitalGause.Text = string.Format("{0:HH:mm:ss}", DateTime.Now);
        }
    }
}